package org.crystalrange.documents.web;

import org.crystalrange.documents.model.DocumentVersionFile;
import org.crystalrange.files.model.File;
import org.crystalrange.softwares.model.Software;
import org.crystalrange.softwares.model.SoftwareVersion;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zul.Bandbox;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class DocumentVersionFileController extends GenericForwardComposer {

	private DocumentVersionFile documentVersionFile;
	private String cultureInfo;
	private File file;
	private SoftwareVersion softwareVersion;
	protected Bandbox bandboxFile;
	protected Bandbox bandboxSoftwareVersion;
	protected AnnotateDataBinder binder;

	public DocumentVersionFile getDocumentVersionFile() {
		return documentVersionFile;
	}

	public void setDocumentVersionFile(DocumentVersionFile documentVersionFile) {
		this.documentVersionFile = documentVersionFile;
	}

	@Override
	public void doAfterCompose(Component component) throws Exception {
		super.doAfterCompose(component);
		this.documentVersionFile = (DocumentVersionFile) this.execution.getAttribute("documentVersionFile");
		
		this.saveData();
		
		component.setAttribute("controller", this);
	}

	public void onClick$buttonOk(Event event) throws Exception {
		if ((this.documentVersionFile.getCultureInfo() != null) && 
			(this.documentVersionFile.getFile() != null) && 
			(this.documentVersionFile.getSoftwareVersion() != null)) {
			this.execution.setAttribute("documentVersionFile",this.documentVersionFile);
			this.execution.setAttribute("result", "ok");
			this.self.detach();
		} else {
			Messagebox.show("You must enter 'Culture Info', select 'File' and 'Software Version'.");
		}
	}

	public void onClick$buttonCancel(Event event) throws Exception {
		this.restoreData();
		
		this.execution.setAttribute("documentVersionFile", null);
		this.execution.setAttribute("result", "cancel");
		this.self.detach();
	}

	public void onOpen$bandboxFile(Event event) throws Exception {
		Window window = (Window) this.execution.createComponents("~./zul/html/fileSelect.zul", null, null);
		window.setParent(self);
		window.doModal();

		if (this.execution.getAttribute("result") == "ok") {
			File selectedFile = (File) this.execution.getAttribute("selectedFile");
			this.documentVersionFile.setFile(selectedFile);
		}

		this.binder.loadAll();
	}

	public void onOpen$bandboxSoftwareVersion(Event event) throws Exception {
		Window window = (Window) this.execution.createComponents("~./zul/html/softwareSelect.zul", null, null);
		window.setParent(self);
		window.doModal();
		
		if (this.execution.getAttribute("result") == "ok") {
			Software selectedSoftware = (Software) this.execution.getAttribute("selectedSoftware");
			this.execution.setAttribute("software", selectedSoftware);
			
			window = (Window) this.execution.createComponents("~./zul/html/softwareVersionSelect.zul", null, null);
			window.setParent(self);
			window.doModal();
			
			if (this.execution.getAttribute("result") == "ok") {
				SoftwareVersion selectedSoftwareVersion = (SoftwareVersion) this.execution.getAttribute("selectedSoftwareVersion");
				this.documentVersionFile.setSoftwareVersion(selectedSoftwareVersion);
			}
		}
		
		this.binder.loadAll();
	}
	
	private void saveData() {
		this.cultureInfo = this.documentVersionFile.getCultureInfo();
		this.file = this.documentVersionFile.getFile();
		this.softwareVersion = this.documentVersionFile.getSoftwareVersion();
	}

	private void restoreData() {
		this.documentVersionFile.setCultureInfo(this.cultureInfo);
		this.documentVersionFile.setFile(this.file);
		this.documentVersionFile.setSoftwareVersion(this.softwareVersion);
	}
}
